import {Component, EventEmitter, Injector, OnInit, Output} from '@angular/core';
import {NzCascaderOption} from 'ng-zorro-antd/cascader';
import {CoreComponent, DynClass} from 'pengesoft-ng-lib';
import {DeptServiceSvr} from '../../../services/dept.service';
import {EmployeeServiceSvr} from '../../../services/employee.service';
import {MyDepartments} from '../../../domains/mydepartments.domain';
import {MyDepartment} from '../../../domains/mydepartment.domain';

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.less']
})
export class SearchComponent extends CoreComponent implements OnInit {

  @Output() outer = new EventEmitter<any>();

  options: any[] = [];

  nzOptions: any[] = [];
  values: string[] | null = null;
  searchInputValue: string[] | null = null;
  // 传给roster的值
  sendRosterSearch: any;
  postingValue: string[] | null = null;
  identifyCardList = [];

  constructor(private injector: Injector,
              private deptSvr: DeptServiceSvr,
              private employSvr: EmployeeServiceSvr) {
    super(injector);
  }

  ngOnInit(): void {
   this.querySearchPostAndDept();
  }
  // 下卡框变化
  onChanges(values: string[]): void {
    this.deptSvr.getAllJobByDept(values[values.length - 1]).then(res => {
      this.identifyCardList = res.map(item => {
        return {
          label: item.JobName, value: item.Id ,
        };
      });
    });
  }
  // 按下搜索按钮
  searchRosterButton(): void {
    const map = new DynClass() ;
    map.jobId = this.postingValue ? this.postingValue.value : null;
    map.employeeName = this.searchInputValue ;
    this.searchInputValue = null;
    this.employSvr.myGetEmployeeDtoByCondition(map).then(res => {
      this.outer.emit(res);
    }) ;
  }
  // 查询搜索栏的岗位和部门
  querySearchPostAndDept(): void {
    this.deptSvr.getAllMyDepartment().then(res => {
      this.options = this.traversal([res] as unknown as MyDepartment[]);

      this.nzOptions = this.options;
    });
  }
  changeChild(): boolean {
    return true;
  }

  // 遍历
  traversal(arr: MyDepartment[]): any{
    return arr.map((item => {
      return {
        value: item.NodeId,
        label: item.DeptName,
        isLeaf: (item.ChildDepartment as unknown as MyDepartment[]).length === 0,
        // tslint:disable-next-line:max-line-length
        children: (item.ChildDepartment as unknown as MyDepartment[]).length === 0 ? [] : this.traversal(item.ChildDepartment as unknown as MyDepartment[])
      };
    }));
  }

  changeOptionValue(values: string[]): void {
  }

}
